import './assets/main.css'

import { createApp } from 'vue'
import App from './App.vue'
import { LogType, type ApiLogDataDto, type JSLogDataDto } from './utils/indexedDB.type';
import { IndexedDBHelper } from './utils/indexedDB';
import axiosInstance from './utils/axio';
import { uploadLogs } from './utils/cosUpload';

const app = createApp(App);

// 将日志管理 实例挂载到 Vue 应用
app.config.globalProperties.$indexDBApi = new IndexedDBHelper<ApiLogDataDto>('logdb_api', 'store_api');
app.config.globalProperties.$indexDBJs = new IndexedDBHelper<ApiLogDataDto>('logdb_js', 'store_js');

// 将 Axios 实例挂载到 Vue 应用
app.config.globalProperties.$http = axiosInstance;

//JS异常捕获
app.config.errorHandler = (err: any, instance, info) => {
    //前端处理报错
    alert(err.message);

    //js报错 本地存储日志
    const dbHelper = new IndexedDBHelper<JSLogDataDto>('logdb_js', 'store_js');
    dbHelper.addData({
        stack: err.stack,
        component: instance?.$options?.__file || instance?.$options?.name,
        hook: info,
        logType: LogType.ERROR,
    }).then(() => {
        //js报错 上报日志
        // dbHelper.getAllData().then((res: any[]) => {
        //     uploadLogs(JSON.stringify(res), `${dbHelper.dbName}_${dbHelper.storeName}`, (path: string) => {
        //         console.log("JS异常捕获日志上传成功，路径：" + path);
        //     });
        // });
    });
}

app.mount('#app')
